<?php

namespace app\admin\model;
use think\Db;
use think\Model;
use my\Security;

class UserIntegral extends Model {

    public function __construct() {
        parent::__construct();
    }

    //用户积分列表
    public function getListInfo($page, $limit, $data) {
        $where = [];
        $where['a.status'] = 1;
        if(!empty($data['user_phone'])){
            $where['b.user_phone'] = Security::encrypt($data['user_phone']);
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('integral_account')->alias('a')->join('user b','a.uid = b.uid','left')->where($where)->count();
        $list = Db::name('integral_account')->alias('a')->join('user b','a.uid = b.uid','left')->where($where)->field('a.*,b.user_phone')->limit($start, $limit)->order('id desc')->select();
        foreach ($list as $key=>$value){
            $list[$key]['add_time']   = date("Y-m-d H:i:s",$value['add_time']);
            $list[$key]['user_phone'] = Security::decrypt($value['user_phone']);
            $list[$key]['unpaid_receive_amount'] = $value['account_receive_amount'] > $value['total_receive_amount'] ?0:$value['total_receive_amount'] - $value['account_receive_amount'];
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }

    //用户积分明细
    public function getListDetail($page, $limit, $data){
        $where = [];
        //手机号搜索
        if(!empty($data['user_phone'])){
            $where['b.user_phone'] = Security::encrypt($data['user_phone']);
        }
        //状态
        if(!empty($data['statusType'])){
            if($data['statusType'] == 1){
                $where['a.amount'] = ['>',0];
            }else{
                $where['a.amount'] = ['<',0];
            }
        }
        //到账状态
        if(!empty($data['is_status'])){
            if($data['is_status'] == 1){
                $where['a.status'] = 1;
            }else{
                $where['a.status'] = 0;
            }
        }
        if(!empty($data['uid'])){
            $where['a.uid'] = $data['uid'];
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('integral_record')->alias('a')->join('user b','a.uid = b.uid','left')->where($where)->count();
        $list = Db::name('integral_record')->alias('a')->join('user b','a.uid = b.uid','left')->where($where)->field('a.*,b.user_phone')->limit($start, $limit)->order('id desc')->select();
        foreach ($list as $key=>$value){
            $list[$key]['add_time']             = date("Y-m-d H:i:s",$value['add_time']);
            $list[$key]['account_time']         = !empty($value['account_time'])?date("Y-m-d H:i:s",$value['account_time']):'-';
            $list[$key]['common_id']            = !empty($value['common_id'])?$value['common_id']:'-';
            $list[$key]['user_phone']           = Security::decrypt($value['user_phone']);
            $list[$key]['statusName']           = !empty($value['status'])?"<span style='color: green'>已到账</span>":"<span style='color: red'>未到账</span>";
            $list[$key]['typeName']             = integralType($value['type']);
            $list[$key]['original_amount']      = $value['original_amount']/100;
            $list[$key]['orginal_selling_price']= $value['orginal_selling_price']/100;
            $list[$key]['orginal_cost_price']   = $value['orginal_cost_price']/100;
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }

}
